package com.gmail.viktor.yuryev;

import com.gmail.viktor.yuryev.model.Link;
import com.gmail.viktor.yuryev.model.ServerDetails;
import com.gmail.viktor.yuryev.strategy.AbstractUploadStrategy;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

import java.util.List;

/**
 * Created by Viktor Yuryev on 4/5/2014.
 */
public class SSHClient {

    ServerDetails connectionData;
    private Session session;

    private AbstractUploadStrategy connection;

    public SSHClient(ServerDetails connectionData) {
        this.connectionData =connectionData;
    }


    public void connect() throws Exception {
        JSch ssh = new JSch();
        session = ssh.getSession(connectionData.getLogin(), connectionData.getHost(), connectionData.getPort());
        session.setPassword(connectionData.getPassword());
        session.setConfig("StrictHostKeyChecking", "no");
        session.connect();
        System.out.println("Connected");
        connection = AbstractUploadStrategy.resolve(session, connectionData);
    }


    public void upload(List<String> files) throws Exception {
        List<Link> mapping = connectionData.getMapping();
        FileUtils utils = new FileUtils(files, mapping);
        utils.generateFileList();
        String s = utils.zipIt();
        connection.upload(s, connectionData.getServerHome());

    }

    public void disconnect() throws Exception {
        if (session != null && session.isConnected()) {
            connection.execCmdReturnScreenOutput("logout");
            session.disconnect();
            System.out.println("session was disconnected");
        }
    }


    public static void main(String[] args) {

     /*   List<String> files = Arrays.asList("C:\\SVN\\Boxster\\tools_common\\site\\solutions\\customers\\sprint\\internaltools\\inventoryjsp\\e2743");

        Map<Path, Path> mapping = MyRemoteServers.getInstance().getServerList().get(0).getMapping();
        FileUtils utils = new FileUtils(files, mapping);
        utils.generateFileList();
        String s = utils.zipIt();*/


    }




    public Session getSession() {
        return session;
    }
}
